#include <opencv2\opencv.hpp>
#include <vector>
#include <iostream>
#include <iomanip>
#include"main.h"

__declspec(dllimport) std::vector<std::vector<bool>> scratch_find(std::vector<std::vector<cv::Mat>> images);


void cutImage(cv::Mat src, int m, int n, int x, int y, cv::Rect& Roi, cv::Mat &dst)
{
	Roi.width = m;
	Roi.height = n;
	Roi.x = x;
	Roi.y = y;
	dst = src(Roi);
}

void fillRect(cv::Mat src, cv::Rect r, cv::Scalar color)
{
	cv::Point points[1][4];
	points[0][0] = cv::Point(r.x, r.y);
	points[0][1] = cv::Point(r.x + r.width, r.y);
	points[0][2] = cv::Point(r.x + r.width, r.y + r.height);
	points[0][3] = cv::Point(r.x, r.y + r.height);

	const cv::Point* ppt[1] = { points[0] };
	int npt[] = { 4 };
	cv::fillPoly(src, ppt, npt, 1, color);
}


//ge yi ban jie qu huahen 

//std::vector<std::vector<cv::Rect>>CheckScratch(std::vector<cv::Mat> beDetectImg)
//{
//	std::vector<std::vector<cv::Mat>> InfoImage;
//	std::vector<std::vector<cv::Rect>> InfoRect;
//	for (int m = 0; m < beDetectImg.size(); m++)
//	{
//		int size_width = 200;
//		int size_height = 200;
//		int index_x = 0;
//		int index_y = 0;
//		int x_max = cvCeil(beDetectImg[m].cols / (size_width / 2));
//		int y_max = cvFloor(beDetectImg[m].rows / (size_height / 2));
//		std::vector<cv::Mat> vecImgTemp;
//		std::vector<cv::Rect> vecRectTemp;
//		for (int i = 0; i < x_max; i++)
//		{
//			if (i < (x_max - 1))
//			{
//				index_x = i * (size_width / 2);
//			}
//			else
//			{
//				index_x = beDetectImg[m].cols - size_width;
//			}
//			for (int j = 0; j < y_max; j++)
//			{
//				if (j < (y_max - 1))
//				{
//					index_y = j * (size_height / 2);
//				}
//				else
//				{
//					index_y = beDetectImg[m].rows - size_height;
//				}
//				cv::Rect Roi = {};
//				cv::Mat Image_cut;
//				cutImage(beDetectImg[m], size_width, size_height, index_x, index_y, Roi, Image_cut);
//				vecImgTemp.push_back(Image_cut);
//				vecRectTemp.push_back(Roi);
//
//			}
//		}
//		InfoImage.push_back(vecImgTemp);
//		InfoRect.push_back(vecRectTemp);
//	}
//	std::vector<std::vector<bool>>res = {};
//	res = scratch_find(InfoImage);
//	std::vector<std::vector<cv::Rect>> resRect;
//	char file_dst[100] = "";
//	for (int m = 0; m < res.size(); m++)
//	{
//		std::vector<cv::Rect> resRectTemp;
//		for (int n = 0; n < res[m].size(); n++)
//		{
//			if (res[m][n] == false)
//			{
//				resRectTemp.push_back(InfoRect[m][n]);
//			}
	/*	else
		{
			sprintf(file_dst, "C:/Users/liwenjun/Desktop/0/test_%d_%d.png", (m + 0), (n + 0));
			imwrite(file_dst, beDetectImg[m](InfoRect[m][n]));

		}*/
//		}
//		resRect.push_back(resRectTemp);
//	}
//
//	return resRect;
//
//}


std::vector<std::vector<cv::Rect>>CheckScratch(std::vector<cv::Mat> beDetectImg)
{
	std::vector<std::vector<cv::Mat>> InfoImage;
	std::vector<std::vector<cv::Rect>> InfoRect;
	for (int m = 0; m < beDetectImg.size(); m++)
	{
		int size_width = 200;
		int size_height = 200;
		int index_x = 0;
		int index_y = 0;
		int x_max = cvCeil(beDetectImg[m].cols / size_width);
		int y_max = cvFloor(beDetectImg[m].rows / size_height);
		std::vector<cv::Mat> vecImgTemp;
		std::vector<cv::Rect> vecRectTemp;
		for (int i = 0; i <= x_max; i++)
		{
			if (i < x_max)
			{
				index_x = i * size_width;
			}
			else
			{
				index_x = beDetectImg[m].cols - size_width;
			}
			for (int j = 0; j <= y_max; j++)
			{
				if (j < y_max)
				{
					index_y = j * size_height;
				}
				else
				{
					index_y = beDetectImg[m].rows - size_height;
				}
				cv::Rect Roi = {};
				cv::Mat Image_cut;
				cutImage(beDetectImg[m], size_width, size_height, index_x, index_y, Roi, Image_cut);
				vecImgTemp.push_back(Image_cut);
				vecRectTemp.push_back(Roi);

			}
		}
		InfoImage.push_back(vecImgTemp);
		InfoRect.push_back(vecRectTemp);
	}
	std::vector<std::vector<bool>>res = {};
	res = scratch_find(InfoImage);
	std::vector<std::vector<cv::Rect>> resRect;
	for (int m = 0; m < res.size(); m++)
	{
		std::vector<cv::Rect> resRectTemp;
		for (int n = 0; n < res[m].size(); n++)
		{
			if (res[m][n] == false)
			{
				resRectTemp.push_back(InfoRect[m][n]);
			}
		}
		resRect.push_back(resRectTemp);
	}

	return resRect;

}



int main()
{

	cv::String pattern = "C:/Users/liwenjun/Desktop/1/*.tiff";
	std::vector<cv::String> fn;
	std::vector<cv::Mat> src2;
	glob(pattern, fn, false);
	size_t count = fn.size();
	char file_dst[100] = "";
	for (int i = 0; i<count; ++i)
	{
		cv::Mat dst;
		dst = imread(fn[i], 0);
		src2.push_back(dst);
	}
	std::vector<std::vector<cv::Rect>> d;
	time_t begin = clock();
	d = CheckScratch(src2);
	std::cout << "time:" << (double)(clock() - begin) / CLOCKS_PER_SEC << std::endl;
	time_t begin1 = clock();
	std::vector<cv::Mat> dstRes;
	for (int m = 0; m < src2.size(); m++)
	{
		cv::Mat dst(src2[m].size(), CV_8UC3, cv::Scalar::all(0));
		cv::cvtColor(src2[m], dst, CV_GRAY2BGR);
		for (int n = 0; n < d[m].size(); n++)
		{

			cv::rectangle(dst, cvPoint(d[m][n].x, d[m][n].y), cvPoint(d[m][n].x + d[m][n].width, d[m][n].y + d[m][n].height), cvScalar(0, 0, 255), 3, 4, 0);
		}
		dstRes.push_back(dst);
	}
	for (int i = 0; i < dstRes.size(); i++)
	{
		sprintf(file_dst, "C:/Users/liwenjun/Desktop/2/test_%d.tiff", (i + 0));
		imwrite(file_dst, dstRes[i]);
	}
	//cv::resize(dstRes[1], dstRes[1], cv::Size(648, 500));
	//imshow("detectImage", dstRes[1]);
	//cvWaitKey(0);
	//system("pause");
	return 0;

}


//cropcropcropcropcropcropcropcropcropcropcropcropcropcropcropcropcropcropcrop
//int main()
//{
//
//	cv::String pattern = "C:/Users/liwenjun/Desktop/big1/*.tiff";
//	std::vector<cv::String> fn;
//	std::vector<cv::Mat> src2;
//	glob(pattern, fn, false);
//	size_t count = fn.size();
//	char file_dst[100] = "";
//	for (int i = 0; i<count; ++i)
//	{
//		cv::Mat dst;
//		dst = imread(fn[i], 0);
//		src2.push_back(dst);
//	}
//	std::vector<std::vector<cv::Rect>> d;
//	time_t begin = clock();
//	d = CheckScratch(src2);
//	std::cout << "time:" << (double)(clock() - begin) / CLOCKS_PER_SEC << std::endl;
//	time_t begin1 = clock();
//	std::vector<cv::Mat> dstRes;
//	for (int m = 0; m < src2.size(); m++)
//	{
//		cv::Mat dst(src2[m].size(), CV_8UC1, cv::Scalar::all(0));
//		for (int n = 0; n < d[m].size(); n++)
//		{
//			sprintf(file_dst, "C:/Users/liwenjun/Desktop/1/test_%d_%d.png", (m+0),(n+0));
//			imwrite(file_dst, src2[m](d[m][n]));
//			
//		}
//	}
//	return 0;
//
//}






